package com.maven.demo.controller;
/*
 * http://www.cnblogs.com/sunniest/p/4555801.html   学习地址
 * */
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.maven.demo.model.Person;
import com.maven.demo.model.Student;
import com.maven.demo.model.User;
import com.maven.demo.service.UserService;

@Controller
@RequestMapping("/mvc")
public class HelloWorldController {
//	@Autowired  
//	private DataSource dataSource;
	
	@Autowired
	private UserService userService;

	//http://127.0.0.1:8088/maven-demo/mvc/helloworld
    @RequestMapping("/helloworld")
    public String sayHello(){
        System.out.println("helloworld");
        return "helloword";
    }
    
    //http://127.0.0.1:8088/maven-demo/mvc/logintest
    @RequestMapping("/logintest")
    public String loginTest(){
    	System.out.println("logintest");
        return "logintest";
    }
    
    /*
     * 四、自动匹配参数
     * get方式如下：http://127.0.0.1:8088/maven-demo/mvc/person?name=1&age=2
     * */
 	//TODO post采用jquery，需要补充
    @RequestMapping("/person")
    public String toPerson(String name,double age){
        System.out.println(name+" "+age);
        return "person";
    }
    
    /*
     * 五、自动装箱
     * get方式如下：http://127.0.0.1:8088/maven-demo/mvc/person1?name=1&age=2
     * post采用jquery，需要补充
     * */
    
    //TODO post采用jquery，需要补充
    @RequestMapping("/person1")
    public String toPerson(Person p){
        System.out.println(p.getName()+" "+p.getAge());
        return "person";
    }
    
    /*
     * 七、向前台传递参数
     * get方式如下：http://127.0.0.1:8088/maven-demo/mvc/show
     * */
    //TODO 前端获得参数后，如何处理需要补充 ,post采用jquery，需要补充
    @RequestMapping("/show")
    public String showPerson(Map<String,Object> map){
        Person p	=	new Person();
        map.put("p", p);
        p.setAge(20);
        p.setName("jayjay");
        return "show";
    }
    
    /*
     * 八、使用Ajax调用
     * GET 方式如下：http://127.0.0.1:8088/maven-demo/mvc/getPerson?name=1
     * */
    //TODO post/ajax采用jquery，需要补充
    @RequestMapping("/getPerson")
    public void getPerson(String name,PrintWriter pw){
        pw.write("hello,"+name);        
    }
    @RequestMapping("/name")
    public String sayHello1(){
        return "name";
    }
    
    /*
     * 九、在Controller中使用redirect方式处理请求
     * GET 方式如下：http://127.0.0.1:8088/maven-demo/mvc/redirect
     * */
    //TODO 访问失败需要查原因
    @RequestMapping("/redirect")
    public String redirect(){
        return "redirect:redirect";
    }
    
    //TODO 上传文件尚未编写
   
    
    
    /*
     * 十一、使用@RequestParam注解指定参数的name
     * http://127.0.0.1:8088/maven-demo/mvc/param?id=2&name=name   
     * */
    //TODO post/ajax采用jquery，需要补充
    @RequestMapping(value="/param")
    public String testRequestParam(@RequestParam(value="id") Integer id,
            @RequestParam(value="name")String name){
        System.out.println(id+" "+name);
        return "/person";
    }  
    
    /*
     * 返回json数据
     * http://127.0.0.1:8088/maven-demo/mvc/user
     * */
    @RequestMapping("/user")
    public @ResponseBody User get(){
        User u = new User();
        u.setId(1);
        u.setName("jayjay");
        return u;
    }

    
    /*
     * 处理本类的异常
     * http://127.0.0.1:8088/maven-demo/mvc/error
     * */
    //TODO 需要补充全局的异常处理
    @ExceptionHandler
    public ModelAndView exceptionHandler(Exception ex){
        ModelAndView mv = new ModelAndView("error");
//        mv.addObject("exception", ex);
        System.out.println("in testExceptionHandler");
        return mv;
    }
    
    @RequestMapping("/error")
    public String error(){
        int i = 5/0;
        System.out.println(i);
        return "hello";
    }
    
    /*
     * 返回json数据
     * http://127.0.0.1:8088/maven-demo/mvc/userservice
     * */
    @RequestMapping("/userservice")
    public @ResponseBody User getUser(){
        return userService.getUser();
    }
    
    /*
     * 测试dbutils
     * http://127.0.0.1:8088/maven-demo/mvc/dbutils
     * */
    @RequestMapping("/dbutils")
    public @ResponseBody List<Student> dbutils() throws SQLException{
    	return userService.getStudent();
    }
 
}
